<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default">
      <slot />
    </span>
  </div>
</template>
<script setup>
import { ref, defineProps, watch, defineEmits } from 'vue'
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})
// 父组件的自定义事件
const emit = defineEmits(['update:modelValue'])

const checked = ref(null)
 // 使用侦听器，得到父组件传递数据，给checked数据
watch(() => props.modelValue, (newVal) => {
  checked.value = newVal
}, { immediate: true })
const changeChecked = () => {
  // 触发父组件去修改选中状态
  emit('update:modelValue', !checked.value)
}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;

  .icon-checked {
    color: @xtxColor;

    ~span {
      color: @xtxColor;
    }
  }

  i {
    position: relative;
    top: 1px;
  }

  span {
    margin-left: 2px;
  }
}
</style>